package com.etc.controller;

import com.alipay.api.AlipayApiException;
import com.etc.entity.Orders;
import com.etc.service.OrderService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;


import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @author 赵伟伟
 */
@CrossOrigin
@Controller
@Api(tags = "订单管理相关接口")
@RequestMapping("/OrderController")
public class OrderController {

    @Autowired
    OrderService orderService;

    /**
     * 动态查询订单信息
     * @return 订单集合
     */
    @RequestMapping("/get")
    @ApiOperation("动态查询订单")
    @ResponseBody
    public PageInfo<Orders> getOrders(@RequestParam String oTitle,
                                        @RequestParam int pageNum,
                                        @RequestParam int size){
        PageInfo<Orders> pageInfo = orderService.getOrders(oTitle, pageNum, size);
        return pageInfo;
    }

    /**
     * 修改订单状态
     * @return 订单集合
     */
    @RequestMapping("/update")
    @ApiOperation("修改订单状态")
    @ResponseBody
    public PageInfo<Orders> updateOrders(@RequestParam Integer oStatus,
                                         @RequestParam String oId,
                                         @RequestParam String oTitle,
                                         @RequestParam int pageNum,
                                         @RequestParam int size){
        PageInfo<Orders> pageInfo = orderService.updateOrders(oStatus, oId, oTitle, pageNum, size);
        return pageInfo;
    }

    /**
     * 查询所有的订单（全部订单列表）
     * @return 订单集合
     */
    @RequestMapping("/queryAll")
    @ApiOperation("全部订单")
    @ResponseBody
    public Map<String,Object> queryAllOrder(){
        List<Orders> list = orderService.getAllOrders();
        for (Orders orders : list) {
            System.out.println(orders);
        }
        HashMap<String, Object> map = new HashMap<>();
        map.put("list",list);
        return map;
    }

    /**
     * 根据订单编号查询订单（指定订单编号）
     * @return
     */
    @RequestMapping("/queryById")
    @ApiOperation("根据ID查询订单")
    @ResponseBody
    public Orders queryOrderById(@RequestParam("oId") String oId){
        Orders orders = orderService.getOrdersById(oId);
        return orders;
    }

    /**
     *
     * 根据订单编号删除订单
     * @return   true:删除成功，false:删除失败
     */
    @RequestMapping("/delById")
    @ApiOperation("根据ID删除订单")
    @ResponseBody
    public boolean delOrderById(@RequestParam("oId") String oId){
        boolean flag = orderService.delById(oId);
        return flag;
    }

    /**
     * 新增订单（用于用户下单）
     * @return true:下单成功，false:下单失败
     */
    @RequestMapping("/insertById")
    @ApiOperation("新增订单")
    @ResponseBody
    public boolean insertOrder(@RequestParam("bId") Integer bId,
                               @RequestParam(value = "oTitle",defaultValue = "") String oTitle,
                               @RequestParam("oNum") Integer oNum,
                               @RequestParam("totalFee") double totalFee,
                               @RequestParam(value = "oImg",defaultValue = "") String oImg){
        boolean flag = orderService.addOrder(bId,oTitle,oNum,totalFee,oImg);
        return flag;
    }



}
